<%@ page isELIgnored="false" %> 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="classes/conndb.jsp"%>
<%@ include file="user_check.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
       <script type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script>
       <meta name="viewport" content="width=device-width, initial-scale=1"/>
       <script type="text/javascript" src="js/reveal/jquery-1.8.0.js"></script>
       <script type="text/javascript" src="js/reveal/jquery.reveal.js"></script>
       <link rel="stylesheet" href="js/reveal/reveal.css" type="text/css"></link>       
       <link href="bower_components/bootstrap/dist/css/bootstrap.min.css" type="text/css" rel="stylesheet"></link>
       <link href="css/page.css" type="text/css" rel="stylesheet"></link>
   </head>
   <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
<body>
<%
   request.setCharacterEncoding("UTF-8");

   String deptname = "";
   String deptid = "";
   String userid = "";
%>

<%
	String timekey = request.getParameter("timekey") == null ? "" : request.getParameter("timekey");//默认：全部
	String wkey = request.getParameter("wkey") == null ? "" : request.getParameter("wkey");
		   //wkey = new String(wkey.getBytes("iso-8859-1"),"UTF-8");
	String selectstatus = request.getParameter("selectstatus") == null ? "1" : request.getParameter("selectstatus");//默认：未办理
	 String start_time = request.getParameter("start_time") == null ? "" : request.getParameter("start_time");
  String end_time = request.getParameter("end_time") == null ? "" : request.getParameter("end_time");
    String operate_type = request.getParameter("operate_type") == null ? "" : request.getParameter("operate_type");
       //operate_type = new String(operate_type.getBytes("iso-8859-1"),"UTF-8");


	String wheresql = " where 1=1 ";
	       
	if(!timekey.equals("-1") && !"".equals(timekey)){
		   if(timekey.equals("1")){
			   wheresql += " and (sysdate - a.operate_time) <=3";
		   }else if(timekey.equals("2")){
			   wheresql += " and (sysdate - a.operate_time) <=5";
		   }else if(timekey.equals("3")){
			   wheresql += " and (sysdate - a.operate_time) <=15";
		   }else if(timekey.equals("4")){
			   wheresql += " and (sysdate - a.operate_time) >30";
		   }
	}
  if(!wkey.equals("")){
       wheresql += " and (a.title like '%"+wkey+"%' or a.content like '%"+wkey+"%' or d.name like '%"+wkey+"%')";
  }
  if(!selectstatus.equals("-1") && !"".equals(selectstatus)){
       wheresql += " and a.status= '"+selectstatus+"'";
  }
  if(!start_time.equals("")){
       wheresql += " and a.operate_time >= to_date('"+start_time+"', 'yyyy-mm-dd hh24:mi:ss')";
  }
  if(!end_time.equals("")){
       wheresql += " and a.operate_time <= to_date('"+end_time+"', 'yyyy-mm-dd hh24:mi:ss')";
  }
  if(!operate_type.equals("-1") && !"".equals(operate_type)){
       wheresql += " and b.type_name ='"+operate_type+"'";
  }
%>


<%
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rss = null;
    String deptsql = "select a.user_id,b.name,b.dept_id from cms_user a join cms_department b on a.dept_id = b.dept_id where a.login_id=?";

    try{
      con = getConnection();
      ps = con.prepareStatement(deptsql);
      ps.setObject(1,login_id);
      rss = ps.executeQuery();
      while (rss.next()) {
           userid = rss.getString("user_id");    	  
           deptname = rss.getString("name");
           deptid = rss.getString("dept_id");
      }
    }catch (Exception e) {
    }finally{
        if(con != null) con.close();
        if(ps != null) ps.close();
        if(rss != null) rss.close();
    }
    if(!login_id.equals("wangji") && !deptname.equals("开普互联")){
        wheresql += " and receive_depart='"+deptid+"'";
    }
%>

<%  
    	Connection conn = null;
    	PreparedStatement pst = null;
    	ResultSet rs = null, rs2 = null, rs3 = null;

    	int pageSize = 12;
    	int currentPage = 1;
    	int total = 0;

    	if ((request.getParameter("currentPage")) != null) {
    		currentPage = Integer.valueOf(request.getParameter("currentPage"));
    	}
    	if (pageSize == 0) {
    		pageSize = 12;
    	}
    	if (currentPage <= 0) {
    		currentPage = 1;
    	}
    	int start = (currentPage - 1) * pageSize;
    	int end = (currentPage - 1) * pageSize + pageSize;

    	String sql = "select a.message_id,a.title,a.messager_name,a.operate_time,a.is_public,a.status,b.type_name,d.name,e.content as answer_content "+
    					"from cms_public_comment a "+
    					"left join cms_operate_type b on a.operate_type_id = b.operate_type_id "+
    					"left join app_user c on a.app_user_guid =c.guid "+
    					"left join cms_department d on a.receive_depart =d.dept_id "+
    					"left join cms_reply_comment e on a.message_id = e.message_id "+wheresql+
    					" order by a.operate_time desc ";
    	
    	try {
    		conn = getConnection();
    		//查询总记录数
    		String countSql = "select count(*) from (" + sql + ")";    		
    		
    		pst = conn.prepareStatement(countSql);
    		rs2 = pst.executeQuery();
    		while (rs2.next()) {
    			total = rs2.getInt(1);
    		}
    		//分页查询
    		String sql1 = "select tt3.* from (select rownum as r,tt2.* from (";
    		String sql2 = ") tt2 where rownum<= ?) tt3 where tt3.r > ? ";
    		String querySql = sql1 + sql + sql2;
    		pst = conn.prepareStatement(querySql);
    		pst.setObject(1, end);
    		pst.setObject(2, start);
    		rs = pst.executeQuery();

    		//封装变量
    		Map pageMap = new HashMap();
    		pageMap.put("currentPage", currentPage);
    		pageMap.put("total", total);
    		pageMap.put("pageSize", pageSize);
    		request.setAttribute("page", pageMap);
        %>
        <div class="container">
  <div class="row">
        <form  action="" method="get" name="form2">
            <table class="table table-striped table-hover table-responsive" width="100%" border="0" cellspacing="0" cellpadding="0">
            <thead>
              <tr>
                <th colspan="2" style="text-align: right;">关键字：<input class="form-control" style="display:inline;width:160px;height:25px;" type="text" name="wkey" placeholder="输入关键字" value="<%=wkey%>"/></th>
                <th colspan="2" style="text-align: center;">
                  时间：
            <select name="timekey" class="form-control" style="display:inline;width:100px;height:31px;">
              <option value="-1" <%if(timekey.equals("-1")) out.print("selected='selected'"); %>>全部</option>
              <option value="1" <%if(timekey.equals("1")) out.print("selected='selected'"); %>>三天内</option>
              <option value="2" <%if(timekey.equals("2")) out.print("selected='selected'"); %>>五天内</option>
              <option value="3" <%if(timekey.equals("3")) out.print("selected='selected'"); %>>半月内</option>
              <option value="4" <%if(timekey.equals("4")) out.print("selected='selected'"); %>>一月以上</option>
            </select>
          </th>
          <th colspan="4">
            状态：
            <select name="selectstatus" class="form-control" style="display:inline;width:100px;height:31px;">
              <option value="-1" <%if(selectstatus.equals("-1")) out.print("selected='selected'"); %>>全部</option>
              <option value="1" <%if(selectstatus.equals("1")) out.print("selected='selected'"); %>>未办理</option>
              <option value="2" <%if(selectstatus.equals("2")) out.print("selected='selected'"); %>>已回复</option>
              <option value="3" <%if(selectstatus.equals("3")) out.print("selected='selected'"); %>>已办理</option>
            </select>
            &nbsp;&nbsp;&nbsp;&nbsp;
            信件类型：
            <select name="operate_type" class="form-control" style="display:inline;width:135px;height:31px;">
              <option value="-1"  <%if(operate_type.equals("-1")) out.print("selected='selected'"); %>>全部</option>
              <option value="领导信箱" <%if(operate_type.equals("领导信箱")) out.print("selected='selected'"); %>>领导信箱</option>
              <option value="书记信箱" <%if(operate_type.equals("书记信箱")) out.print("selected='selected'"); %>>书记信箱</option>
              <option value="12345网络信件" <%if(operate_type.equals("12345网络信件")) out.print("selected='selected'"); %>>12345网络信件</option>
              <option value="建言献策" <%if(operate_type.equals("建言献策")) out.print("selected='selected'"); %>>建言献策</option>
              <option value="在线咨询" <%if(operate_type.equals("在线咨询")) out.print("selected='selected'"); %>>在线咨询</option>
              <option value="网上投诉" <%if(operate_type.equals("网上投诉")) out.print("selected='selected'"); %>>网上投诉</option>
              <option value="公众留言" <%if(operate_type.equals("公众留言")) out.print("selected='selected'"); %>>公众留言</option>
              <option value="县长信箱" <%if(operate_type.equals("县长信箱")) out.print("selected='selected'"); %>>县长信箱</option>
              <option value="网上信访" <%if(operate_type.equals("网上信访")) out.print("selected='selected'"); %>>网上信访</option>
            </select>
          </th>
              </tr>
              <tr>
                <th colspan="5" style="text-align: right;">
                  <span>起始时间：</span>
            <input class="form-control" style="display:inline;width:216px;height:25px;" type="text" name="start_time" readonly="readonly" value="<%=start_time %>" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate:'%y-%M-%d %H%m:%s'})" />
            &nbsp;---&nbsp;    
            结束时间：
            <input class="form-control" style="display:inline;width:216px;height:25px;" type="text" name="end_time" readonly="readonly" value="<%=end_time %>" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate:'%y-%M-%d %H%m:%s'})" />
          </th>
          <th colspan="3">
               <input type="submit" class="btn btn-primary" style="height:30px;background-color: #9EA0A1;border-color: #E1E4E7;" value="搜索" />
          </th>
              </tr>
                <tr>
                    <th width="25%">标题</th>
                    <th width="10%">填写人</th>
                    <th width="15%">填写时间</th>
                    <th width="7%">是否公开</th>
                    <th width="13%">收件单位</th>
                    <th width="10%">信件类型</th>
                    <th width="5%">状态</th>
                    <th width="15%">操作</th>
                </tr>
                </thead>
                <tbody class="">
                <%
                    while (rs.next()) {
                    	String message_id= rs.getString("message_id")== null ? "" : rs.getString("message_id");
                    	String title= rs.getString("title")== null ? "" : rs.getString("title");
            	    	String messager_name= rs.getString("messager_name")== null ? "" : rs.getString("messager_name");
            	    	String operate_time= rs.getString("operate_time")== null ? "" : rs.getString("operate_time").substring(0,rs.getString("operate_time").lastIndexOf(".0"));
            	    	String is_public= rs.getString("is_public")== null ? "" : rs.getString("is_public").replace("1", "是").replace("0", "否");
            	    	String name= rs.getString("name")== null ? "" : rs.getString("name");
            	    	String type_name= rs.getString("type_name")== null ? "" : rs.getString("type_name");
            	    	String status= rs.getString("status")== null ? "" : rs.getString("status").replace("1", "未办理").replace("2", "已回复").replace("3", "已完成");
            	    	String answer_content= rs.getString("answer_content")== null ? "" : rs.getString("answer_content");
                %>
                <tr> 
                    <td><%=title%></td>
                    <td><%=messager_name%></td>
                    <td><%=operate_time%></td>
                    <td style="text-align: center;"><%=is_public%></td>
                    <td><%=name%></td>
                    <td><%=type_name%></td>
                    <td><%=status%></td>
                    <td>
                        <a href="detail.jsp?message_id=<%=message_id %>" title="查看详细" target="_blank"><img src="images/search.png" width="14" /></a>&nbsp;&nbsp;
                        <a href="javascript:if(confirm('确定要删除该条数据？')) {window.location.href='dodel.jsp?message_id=<%=message_id %>'}"  title="删除"><img src="images/delete.png" alt="" width="14" /></a>&nbsp;&nbsp;
                    	
                    	<%if(!status.equals("未办理")) {%>
                    		<a href="edit.jsp?message_id=<%=message_id %>&timekey=<%=timekey %>&wkey=<%=wkey %>&selectstatus=<%=selectstatus %>&start_time=<%=start_time %>&end_time=<%=end_time %>&operate_type=<%=operate_type %>&currentPage=<%=currentPage %>" title="修改"><img src="images/edit.png" alt="" width="14" /></a>&nbsp;&nbsp;
                    	<%}%>
                    	
                    	<%if(status.equals("未办理")) {%>
                    		<a href="#" class="big-link" data-reveal-id="myModal" date-id="<%=message_id %>">回复</a>
                    	<%}%>
                    	
                    	<%if(status.equals("已回复")) {%>
                    		<a href="javascript:docheck('<%=message_id%>');">通过审核</a>&nbsp;&nbsp;
                    	<%}%>
                    </td>
                </tr>
                <%
                }
                       }catch(Exception e){
 			           e.printStackTrace();
 		          }finally{
 		        	   if(rs  !=null) rs.close();
 		        	   if(rs2 !=null) rs2.close();
 		        	   if(rs3 !=null) rs3.close();
 		        	   if(pst !=null) pst.close();
 			           if(conn !=null) conn.close();
 		        }
                %>
                </tbody>
                <tfoot>
                <tr>
                     <td  colspan="8" style="text-align:center;">
                     <ul class="pagination">
                        <jsp:include page="common/page2.jsp" >
                             <jsp:param value="${page.currentPage }" name="currentPage"/>
                             <jsp:param value="${page.total }" name="totalRecord"/>
                             <jsp:param value="${page.pageSize }" name="pageSize"/>
                             <jsp:param value='form2' name="formName"/>
                        </jsp:include>
                        </ul>
                     <input type="hidden" id="timekey" value="<%=timekey%>"/>  
                   <input type="hidden" id="wkey" value="<%=wkey%>"/>  
                   <input type="hidden" id="selectstatus" value="<%=selectstatus%>"/>
                   <input type="hidden" id="start_time" value="<%=start_time%>"/>
                   <input type="hidden" id="end_time" value="<%=end_time%>"/>
                   <input type="hidden" id="operate_type" value="<%=operate_type%>"/>    
                   <input type="hidden" id="currentPage" value="<%=currentPage%>"/>
                   <input type="hidden" id="userid" value="<%=userid%>"/>
                     </td>
                </tr>
                </tfoot>
            </table>
        </form>
        
        <div id="myModal" class="reveal-modal">
           <div>
                 <input type="hidden" id="message_id" />
			</div>
		   <div style="margin: 10px 0;">
		   		是否公开：
		   		<select id="is_public" class="form-control" style="display:inline;width:70px;height:31px;">
					<option value="1" selected="selected">是</option>
					<option value="0" >否</option>
				</select>
			</div>
		   <div style="margin: 10px 0;">
		   		回复时间：
				<input class="form-control" style="display:inline;width:200px;height:25px;" type="text" id="answer_time" readonly="readonly" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate:'%y-%M-%d %H%m:%s'})" />
		   </div>			
           <div style="margin: 10px 0;">
           		回复内容：<font color="red">不要超过2000字</font>
           		<textarea id="content" class="form-control" style=" height: 100px; margin-left: 20px; width: 400px;"></textarea>
           	</div>
           <div style="text-align: center;"><input type="button" class="btn btn-primary" style="margin-right: 10px;" id="sure" value="提交" /><input type="reset" class="btn btn-primary" value="重置" /></div>
           <a class="close-reveal-modal">&#215;</a>
		</div>
        
        </div>
        </div> 

        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script>
        top.$("iframe").height($(document).height()+100);
        $(document).ready(function() {
            //iframe高度自适应
        });
        
        $(".big-link").each(function(){
      	  $(this).on('click',function(){
      	      $("#message_id").val($(this).attr("date-id"));
      	  });
      	});
        
        $("#sure").click(function(){
    	    var message_id = $("#message_id").val();
    	    var is_public = $("#is_public").val();
    	    var answer_time = $("#answer_time").val();
    	    if(answer_time==""){
    	    	alert("请选择回复时间！");
    	    	return;
    	    }
    	    var userid = $("#userid").val();
    	    var content = $("#content").val();
    	    if(content==""){
    	    	alert("请输入回复内容！");
    	    	return;
    	    }
    	    if(content.length>2000){
    	    	alert("回复内容不能超过2000个字！");
    	    	return;
    	    }
    	    var timekey =  $("#timekey").val();
    	    var wkey =  $("#wkey").val();
    	    var selectstatus =  $("#selectstatus").val();
          var start_time = $("#start_time").val();
          var end_time = $("#end_time").val();
          var operate_type = $("#operate_type").val();
    	    var currentPage =  $("#currentPage").val();
    	    
    	    $.post("doanswer.jsp", {
    	      message_id:message_id,
    	      is_public:is_public,
    	      answer_time:answer_time,
    	      content:content,
    	      userid:userid
    	    }, function(data, status) {
    	      //var obj=JSON.parse(data);写出返回类型json时，该语句可省略
    	      if (data.operate_status == "1") {
    	        alert("回复成功！");
    	        $(".close-reveal-modal").click();
    	        window.location.href="list.jsp?timekey="+timekey+"&wkey="+wkey+"&selectstatus="+selectstatus+"&start_time="+start_time+"&end_time="+end_time+"&operate_type="+operate_type+"&currentPage="+currentPage+"&t="+new Date().getTime();
    	      } else {
    	        alert("回复失败！");
    	      }
    	    }, "json");
    	 });
        
        function docheck(val){
            var message_id = val;
    	    var timekey =  $("#timekey").val();
    	    var wkey =  $("#wkey").val();
    	    var selectstatus =  $("#selectstatus").val();
    	    var currentPage =  $("#currentPage").val();
            
            $.post("docheck.jsp", {
              message_id:message_id
            }, function(data, status) {
              //var obj=JSON.parse(data);写出返回类型json时，该语句可省略
              if (data.operate_status == "1") {
                alert("审核成功！");
                window.location.href="list.jsp?timekey="+timekey+"&wkey="+wkey+"&selectstatus="+selectstatus+"&start_time="+start_time+"&end_time="+end_time+"&operate_type="+operate_type+"&currentPage="+currentPage+"&t="+new Date().getTime();
              } else {
                alert("审核失败！");
              }
            }, "json");
          }
        
</script>

</body>
</html>
